package cn.wzg.server.Servers.Serverimpl;

import cn.wzg.server.Servers.Init.NettyServerInitializer;
import cn.wzg.server.Servers.RPCServer;
import cn.wzg.server.Service.ServiceProvider;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpServerChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.AllArgsConstructor;

import java.io.IOException;
import java.util.Map;
@AllArgsConstructor
public class NettyServer implements RPCServer {
    ServiceProvider serviceprovider;
    /***
     * 初始化netty服务器
     * @param port
     * @throws IOException
     */
    @Override
    public void start(int port) throws IOException, InterruptedException {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        NioEventLoopGroup boss = new NioEventLoopGroup();
        ServerBootstrap server = new ServerBootstrap();
        server.group(boss,worker).channel(NioServerSocketChannel.class).
                childHandler(new NettyServerInitializer(serviceprovider));
        //异步阻塞设置
        ChannelFuture channelFuture = server.bind(port).sync();
        System.out.println("netty服务端启动");
        // 死循环监听
        channelFuture.channel().closeFuture().sync();
    }

    @Override
    public void stop() {

    }
}
